package com.tailyou.hms.firstapp.act;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.widget.ImageView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.tailyou.hms.firstapp.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CameraActivity extends AppCompatActivity {

    //解决startActivityForResult过时的问题
    //调用摄像
    private ActivityResultLauncher launcherTP;
    //调用相册
    private ActivityResultLauncher launcherSP;
    private File outputImage;
    private Uri imageUri;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        imageView = findViewById(R.id.ivPic);
        launcherTP = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK) {
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        launcherSP = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                try {
                    Uri uri = result.getData().getData();
                    ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(uri, "r");
                    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.btnSxt).setOnClickListener(v -> takePhoto());
        findViewById(R.id.btnXc).setOnClickListener(v -> selectPicture());
    }

    /**
     * 从相册选择照片
     */
    private void selectPicture() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        launcherSP.launch(intent);
    }

    /**
     * 调用摄像头拍照
     */
    private void takePhoto() {
        try {
            outputImage = new File(getExternalCacheDir(), "sxt" + System.currentTimeMillis());
            outputImage.createNewFile();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
                imageUri = FileProvider.getUriForFile(CameraActivity.this, "com.tailyou.hms.firstapp.fileprovider", outputImage);
            } else {
                imageUri = Uri.fromFile(outputImage);
            }
            //启动需要返回结果的Activity
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            launcherTP.launch(intent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}